package com.zdsoft.crm.dao.auth;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.zdsoft.crm.entity.auth.SysFunction;


public interface ISysFunctionMapper {

	@Insert("insert into sys_function(id,func_code,func_name,func_type,func_url,parent_id)"
			+ " values(#{id},#{func_code},#{func_name},#{func_type},#{func_url,jdbcType=VARCHAR},#{parent_id,jdbcType=VARCHAR})")
	void save(SysFunction func);

	@Select("select * from sys_function")
	List<SysFunction> findAll();

	@Delete("delete from sys_function where id=#{id}")
	void deleteById(String id);

	@Update("update sys_function set func_name=#{func_name},func_type=#{func_type},func_url=#{func_url,jdbcType=VARCHAR},parent_id=#{parent_id,jdbcType=VARCHAR} where id=#{id}")
	void update(SysFunction func);
	
	/*@Select("select id,func_code,func_name,func_type,func_url,parent_id,update_time from sys_function "
			+ "limit #{startIndex},#{pageSize}")
	List<SysFunction>pageList(@Param("startIndex") Integer startIndex,
			@Param("pageSize") Integer pageSize);*/
	
	@Select("SELECT * FROM("
			+ "SELECT A.*,ROWNUM RN "
			+ "FROM "
			+ " (SELECT * FROM SYS_FUNCTION ) A"
			+ " WHERE ROWNUM<=#{endIndex})  TL "
			+ " WHERE RN > #{startIndex}")
	List<SysFunction> pageList(@Param("startIndex") Integer startIndex, @Param("endIndex") Integer endIndex);
	
	@Select("select count(id) from sys_function")
	Long totalCount();
	
	@Select("select * from sys_function where id=#{id}")
	SysFunction findOne(SysFunction func);
	
	@Select("select sf.* from sys_function sf"
			+" inner join sys_role_func srf on srf.func_id = sf.id"
			+" inner join sys_role sr on sr.id = srf.role_id"
			+" inner join sys_user_role sur on sur.role_id = sr.id"
			+" inner join sys_user su on su.id = sur.user_id"
			+" where su.id =#{userId}")
	List<SysFunction> findByUserId(String userId);
	
}
